---
title: "U.N. Membership Time Spans"
author: "Gina Reynolds and Matt Winters"
output: pdf_document
---

This file is used in replicating findings for "Foreign Aid Funnel? Placebo-Based Assessment of Aid Flows to  Non-Permanent U.N. Security Council Members" by Evangeline Reynolds (University of Illinois) and Matthew Winters (University of Illinois). 

Data is from U.N. Website, downloaded December, 12 2014, http://www.un.org/en/members/.  Text below on the membership details for countries is quoted directly from the website and appears in italics.

### Read in and clean up base membership data  

```{r ReadInUN Membership Data}
DATA=readLines("./01RawData/UN Member States - CurrentStatesYearJoinUN.txt")

head(DATA)
tail(DATA)
```


```{r Parse Text Input}

Country=c()
StartDate=c()


for (i in 1:length(DATA)){
  
  Characters=strsplit(DATA[i], "")
  
  startNumeric=min(which(Characters[[1]] %in% c(0:9) ))
  
  Country[i]=substr(DATA[i],1,startNumeric-1)
  StartDate[i]=substr(DATA[i],startNumeric, length(Characters[[1]]))
  
}

head(Country)
head(StartDate)

StartDate=as.Date(StartDate, "%d-%m-%Y")

head(StartDate)

DATA=data.frame(Country, StartDate)

head(DATA)
tail(DATA)

DATA$EndDate=Sys.Date()

```


### Account for idiosyncratic membership details.  



#### Accounting for Indonesia's Temporary Withdrawl:   
  

*By letter of 20 January 1965, Indonesia announced its decision to withdraw from the United Nations "at this stage and under the present circumstances".  	By telegram of 19 September 1966, it announced its decision "to resume full cooperation with the United Nations and to resume participation in its activities".		On 28 September 1966, the General Assembly took note of this decision and the President invited representatives of Indonesia to take seats in the Assembly.*  



```{r Edit Indonesia Membership with Detail}
DATA$EndDate[DATA$Country=="Indonesia"]=as.Date("20 January 1965", format="%d %B %Y")

Country="Indonesia"
StartDate=as.Date("28 September 1966", format="%d %B %Y")
EndDate=Sys.Date()
DATA=rbind(DATA,data.frame(Country,StartDate, EndDate))
tail(DATA)
```


#### Countries Combine and Break Up:  

*Egypt 24-10-1945 and Syrian Arab Republic 24-10-1945*

*Egypt and Syria were original Members of the United Nations from 24 October 1945.*
*Following a plebiscite on 21 February 1958, the United Arab Republic was established by the union of Egypt and Syria and became a single member state.*
*On 13 October 1961, Syria, having again become an independent State, resumed its separate membership in the United Nations.*
*On 2 September 1971, the United Arab Republic changed its name to the Arab Republic of Egypt.*



```{r Edit Egypt and Syria and United Arab Republic Entries}
length(DATA$EndDate[DATA$Country=="Egypt"])
length(DATA$EndDate[DATA$Country=="Syrian Arab Republic"])
DATA$EndDate[DATA$Country=="Egypt"]=as.Date("21 February 1958", format="%d %B %Y")
DATA$EndDate[DATA$Country=="Syrian Arab Republic"]=as.Date("21 February 1958", format="%d %B %Y")

Country="United Arab Republic"
StartDate=as.Date("21 February 1958", format="%d %B %Y")
EndDate=as.Date("13 October 1961", format="%d %B %Y")
DATA=rbind(DATA,data.frame(Country,StartDate, EndDate))

Country="Egypt"
StartDate=as.Date("13 October 1961", format="%d %B %Y")
EndDate=Sys.Date()
DATA=rbind(DATA,data.frame(Country,StartDate, EndDate))

Country="Syrian Arab Republic"
StartDate=as.Date("13 October 1961", format="%d %B %Y")
EndDate=Sys.Date()
DATA=rbind(DATA,data.frame(Country,StartDate, EndDate))
tail(DATA)
```




#### Countries Combine:  


*United Republic of Tanzania 14-12-1961*

*Tanganyika was a Member of the United Nations from 14 December 1961 and Zanzibar was a Member from 16 December 1963.*
*Following the ratification on 26 April 1964 of Articles of Union between Tanganyika and Zanzibar, the United Republic of Tanganyika and Zanzibar continued as a single Member, changing its name to the United Republic of Tanzania on 1 November 1964*




```{r Add Zanzibar}

Country="Zanzibar"
StartDate=as.Date("16 December 1963", format="%d %B %Y")
EndDate=as.Date("01 November 1964", format="%d %B %Y")
DATA=rbind(DATA,data.frame(Country,StartDate, EndDate))
tail(DATA)
```

*Yemen 30-09-1947*

*Yemen was admitted to membership in the United Nations on 30 September 1947 and Democratic Yemen on 14 December 1967.*
*On 22 May 1990, the two countries merged and have since been represented as one member with the name "Yemen".*


```{r Add Democratic Yemen}

Country="Democratic Yemen"
StartDate=as.Date("14 December 1967", format="%d %B %Y")
EndDate=as.Date("22 May 1990", format="%d %B %Y")
DATA=rbind(DATA,data.frame(Country,StartDate, EndDate))
tail(DATA)
```
*Germany 18-09-1973*

*The Federal Republic of Germany and the German Democratic Republic were admitted to membership in the United Nations on 18 September 1973.*
*Through the accession of the German Democratic Republic to the Federal Republic of Germany, effective from 3 October 1990, the two German states united to form one sovereign state.*


```{r Correct Germany info to reflect History}
DATA[DATA$Country=="Germany",]
DATA[DATA$Country=="Germany", "StartDate"]=as.Date("03 October 1990", format="%d %B %Y")
DATA[DATA$Country=="Germany",]

```

```{r Add East Germany}
Country="East Germany"
StartDate=as.Date("18 September 1973", format="%d %B %Y")
EndDate=as.Date("02 October 1990", format="%d %B %Y")
DATA=rbind(DATA,data.frame(Country,StartDate, EndDate))
tail(DATA)
```

```{r Add West Germany}
Country="West Germany"
StartDate=as.Date("18 September 1973", format="%d %B %Y")
EndDate=as.Date("02 October 1990", format="%d %B %Y")
DATA=rbind(DATA,data.frame(Country,StartDate, EndDate))
tail(DATA)
```

#### Countries Break Up:

*Malaysia 17-09-1957 and Singapore 21-09-1965*

*The Federation of Malaya joined the United Nations on 17 September 1957.*
*On 16 September 1963, its name was changed to Malaysia, following the admission to the new federation of Singapore, Sabah (North Borneo), and Sarawak.*
*Singapore became an independent state on 9 August 1965 and a member of the United Nations on 21 September 1965.*




*Slovakia 19-01-1993 and Czech Republic 19-01-1993*

*Czechoslovakia was an original Member of the United Nations from 24 October 1945.*
*In a letter dated 10 December 1992, its Permanent Representative informed the Secretary-General that the Czech and Slovak Federal Republic would cease to exist on 31 December 1992 and that the Czech Republic and the Slovak Republic, as successor states, would apply for membership in the United Nations.*
*Following the receipt of their application, the Security Council, on 8 January 1993, recommended to the General Assembly that the Czech Republic and the Slovak Republic be both admitted to United Nations membership. Both the Czech Republic and the Slovak Republic were thus admitted on 19 January of that year as Member States.*


```{r Add czechoslovakia}
Country="Czechoslovakia"
StartDate=as.Date("24 October 1945", format="%d %B %Y")
EndDate=as.Date("31 December 1992", format="%d %B %Y")
DATA=rbind(DATA,data.frame(Country,StartDate, EndDate))
tail(DATA)
```

*South Sudan 14-07-2011*

*The Republic of South Sudan formally seceded from Sudan on 9 July 2011 as a result of an internationally monitored referendum held in January 2011, and was admitted as a new Member State by the United Nations General Assembly on 14 July 2011.*


*Russian Federation 24-10-1945*

*The Union of Soviet Socialist Republics was an original Member of the United Nations from 24 October 1945.*
*In a letter dated 24 December 1991, Boris Yeltsin, the President of the Russian Federation, informed the Secretary-General that the membership of the Soviet Union in the Security Council and all other United Nations organs was being continued by the Russian Federation with the support of the 11 member countries of the Commonwealth of Independent States.*
	
*Yugoslavia:*
	
*Bosnia and Herzegovina  22-05-1992; The former Yugoslav Republic of Macedonia 08-04-1993; Montenegro 28-06-2006; Serbia 01-11-2000; Croatia  22-05-1992; Slovenia 22-05-1992*

*The Socialist Federal Republic of Yugoslavia was an original member of the United Nations, the Charter having been signed on its behalf on 26 June 1945 and ratified 19 October 1945, until its dissolution following the establishment and subsequent admission as new Members of Bosnia and Herzegovina, the Republic of Croatia, the Republic of Slovenia, The former Yugoslav Republic of Macedonia, and the Federal Republic of Yugoslavia.*

*The Republic of Croatia was admitted as a Member of the United Nations by General Assembly resolution A/RES/46/238 of 22 May 1992.*

*The Republic of Bosnia and Herzegovina was admitted as a member of the United Nations by General Assembly resolution A/RES/46/237 of 22 May 1992.*

*The Republic of Slovenia was admitted as a member of the United Nations by General Assembly resolution A/RES/46/236 of 22 May 1992.*

*By resolution A/RES/47/225 of 8 April 1993, the General Assembly decided to admit as a member of the United Nations the State being provisionally referred to for all purposes within the United Nations as "The former Yugoslav Republic of Macedonia" pending settlement of the difference that had arisen over its name.*

*The Federal Republic of Yugoslavia was admitted as a member of the United Nations by General Assembly resolution A/RES/55/12 of 1 November 2000.*

*On 4 February 2003, following the adoption and promulgation of the Constitutional Charter of Serbia and Montenegro by the Assembly of the Federal Republic of Yugoslavia, the official name of " Federal Republic of Yugoslavia" was changed to Serbia and Montenegro.*

*In a letter dated 3 June 2006, the President of the Republic of Serbia informed the Secretary-General that the membership of Serbia and Montenegro was being continued by the Republic of Serbia, following Montenegro's declaration of independence.*

*Montenegro held a 21 May 2006 referendum and declared itself independent from Serbia on 3 June.*
*On 28 June 2006 it was accepted as a United Nations Member State by General Assembly resolution A/RES/60/264.*
				
	


```{r Add Yugoslavia}
Country="Yugoslavia"
StartDate=as.Date("19 October 1945", format="%d %B %Y")
EndDate=as.Date("1 November 2000", format="%d %B %Y")
DATA=rbind(DATA,data.frame(Country,StartDate, EndDate))
tail(DATA)
```


#### Name Changes:



*Democratic Republic of the Congo   20-09-1960*

*Zaire joined the United Nations on 20 September 1960.*
*On 17 May 1997, its name was changed to the Democratic Republic of the Congo.*

*Libya 14-12-1955*

*Following the adoption by the General Assembly of resolution 66/1, the Permanent Mission of Libya to the United Nations formally notified the United Nations of a Declaration by the National Transitional Council of 3 August changing the official name of the Libyan Arab Jamahiriya to "Libya" and changing Libya's national flag.*

*Belarus  24-10-1945*

*On 19 September 1991, the Byelorussian Soviet Socialist Republic informed the United Nations that it had changed its name to Belarus.*
		


### Convert Country Names to Standard Versions


```{r Assign a Standard Name for the Countries}
setwd("./000MyCountryCoder/") #For chunk temporarily resets locaction
source("countrycode2.R")
head(DATA)
DATA$StandardName=FavoriteCountryName(DATA$Country)
head(DATA)
table(DATA$StandardName)[table(DATA$StandardName)>1]
```

```{r Change the name of the Data}
UNMemberData=DATA

```



```{r, UNMembership, fig.height=11}
str(DATA)

par(mar=c(5.1,4.1,4.1,12))

DATA$StandardName=factor(DATA$StandardName, levels=unique(as.character(DATA$StandardName)[order(DATA$StartDate)]))

head(levels(DATA$StandardName))

plot(
  DATA$StartDate,
  DATA$StandardName,
     xlab="",
     ylab="",
     axes=FALSE, 
     pch="",
     main="UN Membership")

abline(v=as.Date(c("1-1-1945","1-1-1965", "1-1-1985", "1-1-2005"), "%d-%m-%Y"), col="grey")


for (i in 1:length(DATA$EndDate)){
  
  lines(c(DATA$StartDate[i],DATA$EndDate[i]), rep(DATA$StandardName[i],2) )
  
}

for (i in 1:length(DATA$EndDate)){
  
if(DATA$EndDate[i]!=2011){
points(DATA$EndDate[i], DATA$StandardName[i], pch=19, col="white")
points(DATA$EndDate[i], DATA$StandardName[i])
}
if(DATA$StartDate[i]!=1816){
points(DATA$StartDate[i], DATA$StandardName[i], pch=19, col="white")
points(DATA$StartDate[i], DATA$StandardName[i])  
}
}

axis(1, at=as.Date(c("1-1-1945","1-1-1965", "1-1-1985", "1-1-2005"), "%d-%m-%Y"), 
     labels=c("1945","1965", "1985", "2005"), las=1, cex.axis=0.7, col="white")
axis(4, at=unique(DATA$StandardName), labels=unique(as.character(DATA$StandardName)), 
     las=2, cex.axis=0.35, col="white")

```


### Create Yearly Data

```{r Make time series UN Membership, results='markup'}
options(width=80)
CurrentYear=as.numeric(substr(Sys.Date(),1,4))
Year=1945:CurrentYear
NumYears=length(Year);NumYears
StandardName=unique(UNMemberData$StandardName)
NumCountries=length(StandardName);NumCountries
CountryYearData=merge(Year,StandardName)
head(CountryYearData)
names(CountryYearData)=c("Year", "StandardName")
dim(CountryYearData); length(StandardName)* length(Year)
```

### Now merge U.N. membership data with the country-year base data


```{r Now Complete Country Year Base}
min(UNMemberData$year)
dim(CountryYearData)
UNMemLong=merge(CountryYearData, UNMemberData, by="StandardName")
dim(UNMemLong)  #some countries that are out and in again result in expansion.
#below we aggregate the dummy variable to fix this.  - Egypt, Indonesia and Syria
```


### Calculate whether or not the country is a member of UN in the time span

```{r Calc if Member of Not}
str(UNMemLong)
table(UNMemLong$StandardName)[table(UNMemLong$StandardName)!=71]

UNMemLong$UNMember=ifelse(UNMemLong$Year>=as.numeric(substr(UNMemLong$StartDate,1,4))&
                          UNMemLong$Year<=as.numeric(substr(UNMemLong$EndDate,1,4)), 1,0)

table(UNMemLong$UNMember, useNA="ifany")


UNMemLong$UNMemberAtStartofYear=ifelse(as.numeric(substr(UNMemLong$StartDate,1,4))<UNMemLong$Year&
                             as.numeric(substr(UNMemLong$EndDate,1,4))>=UNMemLong$Year,
                             1,0)

table(UNMemLong$UNMemberAtStartofYear, UNMemLong$UNMember, useNA="ifany")
length(unique(UNMemLong$StandardName))

#Aggregating on country accounts for countries that are out and in again}

names(UNMemLong)
dim(UNMemLong)
UNMemLong=aggregate(UNMemLong$UNMember, by=list(UNMemLong$Year, UNMemLong$StandardName), FUN=sum )
dim(UNMemLong)
names(UNMemLong)=c("Year", "StandardName", "UNMember")
head(UNMemLong)
table(UNMemLong$StandardName)[table(UNMemLong$StandardName)!=71]
table(UNMemLong$UNMember)

```

### Save Output


```{r save }
write.csv(UNMemLong, "03_UNMemLong.csv")
save(UNMemLong, file="03_UNMemLong.RData")

```

